package com.jic.manager.impl;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.manager.mapper.CmsTemplateVariableMapper;
import com.jic.manager.entity.CmsTemplateVariable;
import com.jic.manager.entity.vo.request.CmsTemplateVariableRequest;
import com.jic.manager.entity.vo.response.CmsTemplateVariableResponse;
import com.jic.manager.service.CmsTemplateVariableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模板变量
 * @author : 
 * @email : 
 * @since : 2020-03-05 12:01:22
 * @version : v1.0.0
 */
@Service
@Slf4j
public class CmsTemplateVariableServiceImpl implements CmsTemplateVariableService{

    @Autowired
    private CmsTemplateVariableMapper cmsTemplateVariableMapper;

    @Override
    public long insert(CmsTemplateVariable cmsTemplateVariable) {
        long flag = cmsTemplateVariableMapper.insert(cmsTemplateVariable);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return cmsTemplateVariableMapper.delete(id);
    }

    @Override
    public long update(CmsTemplateVariable cmsTemplateVariable) {
        return cmsTemplateVariableMapper.update(cmsTemplateVariable);
    }

    @Override
    public CmsTemplateVariable queryByPrimaryKey(Long id){
        return cmsTemplateVariableMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<CmsTemplateVariable> query(CmsTemplateVariable cmsTemplateVariable){
        return cmsTemplateVariableMapper.query(cmsTemplateVariable);
    }

    @Override
    public PageResult<CmsTemplateVariable> queryPage(CmsTemplateVariable cmsTemplateVariable, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<CmsTemplateVariable> list = cmsTemplateVariableMapper.queryPage(startIndex, pageQuery.getLimit(), cmsTemplateVariable);
        long count = cmsTemplateVariableMapper.queryPageCount(cmsTemplateVariable);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    @Override
    public List<CmsTemplateVariableResponse> listCmsTemplateVariableRecordPagination(CmsTemplateVariableRequest request) {
        //分页处理

        CmsTemplateVariable param = new CmsTemplateVariable();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<CmsTemplateVariable> list = cmsTemplateVariableMapper.listCmsTemplateVariableRecordPagination(request.getStart(), request.getLimit(), param);

        long count = cmsTemplateVariableMapper.listPageCount(param);

        List<CmsTemplateVariableResponse> schoolResponseList = list.stream().map(temp -> {
            CmsTemplateVariableResponse schoolResponse = new CmsTemplateVariableResponse();
            BeanUtils.copyProperties(temp, schoolResponse);
            return schoolResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<CmsTemplateVariableResponse> pageResult = new PageResult<>();
        pageResult.setRows(schoolResponseList);
        pageResult.setTotal(count);

        return new ArrayList<>();
    }

    /**
     * 分页查询，获取总记录
     *
     * @param request
     * @return
     */
    @Override
    public long listPageCount(CmsTemplateVariable request) {
        return cmsTemplateVariableMapper.listPageCount(request);
    }


    /**
     * 不分页查询
     *
     * @param request
     * @return
     */
    @Override
    public List<CmsTemplateVariable> listCmsTemplateVariableAllRecord(CmsTemplateVariable request) {
        //分页处理
        request.setDeleteFlag(0);
        return cmsTemplateVariableMapper.selectBySelective(request);
    }

    /**
     * 修改审核
     *
     * @param request
     * @return
     */
    @Override
    public long updateCmsTemplateVariable(CmsTemplateVariable request) throws Throwable {
        try {
            log.info("修改记录");
            CmsTemplateVariable school = new CmsTemplateVariable();
            BeanUtils.copyProperties(request, school);
            long flag = cmsTemplateVariableMapper.updateByPrimaryKey(school);
        } catch (Exception e) {
            log.error("记录审核失败", e);
            throw new Exception("9999");
        }

        return -1;
    }

    /**
     * 记录详情
     *
     * @param request
     * @return
     */
    @Override
    public CmsTemplateVariable getCmsTemplateVariable(CmsTemplateVariable request) {
        return cmsTemplateVariableMapper.selectByPrimaryKey(request);
    }


    /**
     * 新增记录
     *
     * @param request
     * @return
     */
    @Override
    public long addCmsTemplateVariable(CmsTemplateVariable request) {
        return cmsTemplateVariableMapper.insert(request);
    }


    /**
     * 删除记录
     *
     * @param request
     * @return
     */
    @Override
    public long deleteCmsTemplateVariable(CmsTemplateVariable request) {
        return cmsTemplateVariableMapper.deleteByPrimaryKey(request);
    }


    /**
     * 逻辑删除记录
     *
     * @param request
     * @return
     */
    @Override
    public long deleteCmsTemplateVariableLogically(CmsTemplateVariable request) {
        request.setDeleteFlag(1);
        return cmsTemplateVariableMapper.updateByPrimaryKey(request);
    }


    /**
     * 保存或修改记录信息
     * @param request 入参实体类
     * @return String
     * author tzc
     * @date 2020-3-11 20:54:09
     */
    @Override
    public long saveOrUpdateCmsTemplateVariable(CmsTemplateVariable request) throws Throwable {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                new Exception("入参为空");
            }
            //if (StringUtils.isBlank(request.getInstitutionCode())) {
            //return RestResult.error(ChoiceCenterErrorCode.EXCEPTION, "营业执照为空");
            //}
            request.setDeleteFlag(0);
            request.setCreator(request.getCreator());
            if (request !=null && request.getId() != null) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                return  cmsTemplateVariableMapper.updateByPrimaryKey(request);
            }else{
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                return cmsTemplateVariableMapper.insertSelective(request);
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new Exception("9999");
        }
    }

}